/**
 * Created by gongxun on 17/3/21.
 */

var PszTableBipaiLayer = GameCCSBase.extend({
    Panel_main : null,
    Panel_center : null,
    Image_selects : null,
    ctor: function () {
        this._super();
        this.loadCocostudio("res/ccs/PszTableBipai.json");
        this.Panel_main = mo.seekWidgetByName(this.ccsNode, "Panel_main");
        this.Panel_center = mo.seekWidgetByName(this.ccsNode, "Panel_center");

        this.Panel_main.setVisible(false);
        this.Panel_center.setVisible(false);

        this.Image_selects = [null];
        for (var i = 1; i < 5; i++){
            var Image_select = mo.seekWidgetByName(this.ccsNode, "Image_select" + i);
            Image_select.setTag(i);
            this.Image_selects.push(Image_select);

            var Sprite_zhizhen = mo.seekWidgetByName(Image_select, "Sprite_zhizhen");
            var animation = new cc.Animation(this.getZhizhenAnim());
            animation.setDelayPerUnit(0.2);
            Sprite_zhizhen.runAction(cc.repeatForever(cc.animate(animation)));

            Image_select.addTouchEventListener(function(sender, type){
                if (mo.gameHelper.getIsTouchEnd(type)) {
                    mo.pszTableStatus.sendBipaiAction(sender.getTag());
                }
            }, this);
        }
    },

    setStartBipai : function(compares){
        this.Panel_main.setVisible(true);
        this.Panel_center.setVisible(false);

        for (var i = 1; i < 5; i++){
            var Image_select = this.Image_selects[i];
            Image_select.setVisible(compares[i]);
        }
    },

    setEndBipai : function(){
        this.Panel_main.setVisible(false);
        // this.Panel_center.setVisible(true);
    },

    getZhizhenAnim : function(){
        var spriteFrames = [];
        for (var i = 0; i <= 3; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("psz_zhizhen" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        return spriteFrames;
    },

    //比牌
    bipai : function(seat0, seat1, winSeat){
        this.Panel_main.setVisible(false);
        this.Panel_center.setVisible(true);

        var Image_di0 = mo.seekWidgetByName(this.Panel_center, "Image_di0");
        var Image_di1 = mo.seekWidgetByName(this.Panel_center, "Image_di1");
        var Image_pk0 = mo.seekWidgetByName(this.Panel_center, "Image_pk0");
        var Image_pk0_pk = mo.seekWidgetByName(Image_pk0, "Image_pk");
        var Image_pk1 = mo.seekWidgetByName(this.Panel_center, "Image_pk1");
        var Image_pk1_pk = mo.seekWidgetByName(Image_pk1, "Image_pk");
        var Sprite_light = mo.seekWidgetByName(this.Panel_center, "Sprite_light");
        var Text_nick0 = mo.seekWidgetByName(this.Panel_center, "Text_nick0");
        var Text_nick1 = mo.seekWidgetByName(this.Panel_center, "Text_nick1");

        Image_di0.setVisible(true);
        Image_di1.setVisible(true);
        Image_pk0.setVisible(false);
        Image_pk0_pk.setVisible(false);
        Image_pk1.setVisible(false);
        Image_pk1_pk.setVisible(false);
        Sprite_light.setVisible(false);

        //nick
        mo.gameHelper.setTextString(Text_nick0, mo.pszTableStatus.getViewSeatNick(seat0));
        mo.gameHelper.setTextString(Text_nick1, mo.pszTableStatus.getViewSeatNick(seat1));


        //bg
        this.Panel_center.setOpacity(0);
        this.Panel_center.stopAllActions();
        this.Panel_center.runAction(cc.sequence(
            cc.fadeIn(0.2),
            cc.delayTime(1.5 + 1.4 + 0.2),
            cc.fadeOut(0.2),
            cc.hide()
        ));

        //底
        Image_di0.setPosition(cc.p(-mo.gameSize.x / 2, 0));
        Image_di0.stopAllActions();
        Image_di0.runAction(cc.sequence(
            cc.moveTo(0.4, cc.p(80, 0))
        ));

        Image_di1.setPosition(cc.p(mo.gameSize.x / 2, 0));
        Image_di1.stopAllActions();
        Image_di1.runAction(cc.sequence(
            cc.moveTo(0.4, cc.p(-80, 0))
        ));

        //pk
        Image_pk0.setPosition(cc.p(-300, 0));
        Image_pk0.setScale(4);
        Image_pk0.setOpacity(0);
        Image_pk0.stopAllActions();
        Image_pk0.runAction(cc.sequence(
            cc.delayTime(0.5),
            cc.show(),
            cc.spawn(
                cc.moveTo(0.3, cc.p(-85, 0)),
                cc.scaleTo(0.3, 1.6),
                cc.fadeIn(0.3)
            ),
            cc.callFunc(function(){
                Image_pk0_pk.stopAllActions();
                Image_pk0_pk.setScale(1);
                Image_pk0_pk.setOpacity(255);
                Image_pk0_pk.runAction(cc.sequence(
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.3, 1.5),
                        cc.fadeOut(0.15)
                    )
                ));
            })
        ));

        Image_pk1.setPosition(cc.p(300, 0));
        Image_pk1.setScale(4);
        Image_pk1.setOpacity(0);
        Image_pk1.stopAllActions();
        Image_pk1.runAction(cc.sequence(
            cc.delayTime(0.5),
            cc.show(),
            cc.spawn(
                cc.moveTo(0.3, cc.p(85, 0)),
                cc.scaleTo(0.3, 1.6),
                cc.fadeIn(0.3)
            ),
            cc.callFunc(function(){
                Image_pk1_pk.stopAllActions();
                Image_pk1_pk.setScale(1);
                Image_pk1_pk.setOpacity(255);
                Image_pk1_pk.runAction(cc.sequence(
                    cc.show(),
                    cc.spawn(
                        cc.scaleTo(0.3, 1.5),
                        cc.fadeOut(0.15)
                    )
                ));
            })
        ));

        //light 2.9s
        var spriteFrames = [];
        for (var i = 0; i <= 13; i++){
            var frame = cc.spriteFrameCache.getSpriteFrame("psz_light" + i + ".png");
            if (frame == null){
                break;
            }
            spriteFrames.push(frame);
        }
        var animation = new cc.Animation(spriteFrames);
        animation.setDelayPerUnit(0.1);
        Sprite_light.runAction(cc.sequence(
            cc.delayTime(1.5),
            cc.show(),
            cc.callFunc(function(){
                //音效
                mo.soundHelper.playEffectCompare();
            }),
            cc.animate(animation),
            cc.hide()
        ));
    }
});